package cn.flyboy.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.flyboy.mapper.ExamMapper;
import cn.flyboy.mapper.StudentMapper;
import cn.flyboy.entity.Answer;
import cn.flyboy.entity.Exam;
import cn.flyboy.entity.Student;
import cn.flyboy.service.StudentService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;
    @Resource
    private ExamMapper examMapper;

    @Override
    public Student login(Student student) {
        Exam exam = examMapper.selStartExam().get(0);
        student.setStu_exam(exam.getE_name());
        System.out.println("set+++++++++++++++++0" + exam.getE_name());
        return studentMapper.selByStudent(student);
    }

    @Override
    public Student selStudentByIdExam(String stuId, String stuExam) {
        return studentMapper.selStudentByIdExam(stuId, stuExam);
    }

    @Override
    public int editStudent(Student student) {
        return studentMapper.editStudent(student);
    }

    @Override
    public Student selByStudentIP(String stu_id, String stu_ip) {
        Exam exam = examMapper.selStartExam().get(0);
        System.out.println("set+++++++++++++++++0" + exam.getE_name());
        String stu_exam = exam.getE_name();
        return studentMapper.selByStudentIP(stu_id, stu_ip, stu_exam);
    }

    public boolean importExcelToSQL(String fileUrl, String stu_exam) {
        try (FileInputStream inputStream = new FileInputStream(fileUrl)) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue;
                }

                Student student = new Student();

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);

                student.setStu_id(row.getCell(0).getStringCellValue());
                student.setStu_name(row.getCell(1).getStringCellValue());
                student.setStu_class(row.getCell(2).getStringCellValue());
                student.setStu_exam(stu_exam);
                studentMapper.insStudent(student);
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    @Override
    public List<Student> selAllStudents(String e_name, String e_teacher) {
        return studentMapper.selAllStudents(e_name, e_teacher);
    }

    @Override
    public int insStudent(Student student) {
        return studentMapper.insStudent(student);
    }

    @Override
    public List<Student> selStudentById(String stu_id, String e_name) {
        return studentMapper.selStudentById(stu_id, e_name);
    }

    // 批量导入学生信息
    @Override
    public int saveExcel(MultipartFile multipartFile, String e_name, HttpServletRequest req, HttpSession session) {
        // 获取文件名
        String originalFileName = multipartFile.getOriginalFilename();

        // 上传路径保存设置
        String path = req.getServletContext().getRealPath("/WEB-INF/files/" + e_name);

        // (String parent,String Child)
        File targetFilePath = new File(path, originalFileName);

        if (!targetFilePath.exists()) {
            targetFilePath.getParentFile().mkdirs();
            try {
                if (targetFilePath.createNewFile()) {
                    System.out.println(targetFilePath);
                    multipartFile.transferTo(targetFilePath);
                    if (importExcelToSQL(path + File.separator + originalFileName, e_name)) {
                        return 0;
                    } else {
                        return 3;
                    }
                }
            } catch (IOException e) {
                return 2;
            }
        }
        return 1;
    }

    @Override
    public int updUnBind(String stu_id) {
        return studentMapper.updUnBind(stu_id);
    }

    @Override
    public int updStuIp(Student stu) {
        return studentMapper.updStuIp(stu);
    }

    @Override
    public List<Answer> selAllAnswerInfo(String stu_id, String exam_name) {
        return studentMapper.selAllAnswerInfo(stu_id, exam_name);
    }
}
